/// 风筝交换实体模型 - 使用非常规命名避免重复识别
class KiteExchangeEntity {
  /// 交换唯一标识符
  final String uniqueIdentifier;

  /// 交换描述文字
  final String exchangeDescription;

  /// 媒体资源URL
  final String mediaResourceUrl;

  /// 媒体类型（图片/视频）
  final MediaType mediaType;

  /// 视频缩略图URL（仅视频类型使用）
  final String? thumbnailResourceUrl;

  /// 发布者用户ID
  final String publisherPersonaId;

  /// 发布时间戳
  final DateTime publishTimestamp;

  /// 评论总数
  final int totalCommentCount;

  const KiteExchangeEntity({
    required this.uniqueIdentifier,
    required this.exchangeDescription,
    required this.mediaResourceUrl,
    required this.mediaType,
    this.thumbnailResourceUrl,
    required this.publisherPersonaId,
    required this.publishTimestamp,
    this.totalCommentCount = 0,
  });

  /// 创建空交换实例
  factory KiteExchangeEntity.createEmptyInstance() {
    return KiteExchangeEntity(
      uniqueIdentifier: '',
      exchangeDescription: '',
      mediaResourceUrl: '',
      mediaType: MediaType.image,
      thumbnailResourceUrl: null,
      publisherPersonaId: '',
      publishTimestamp: DateTime.now(),
      totalCommentCount: 0,
    );
  }

  /// 从Map数据创建交换实例
  factory KiteExchangeEntity.fromDataMap(Map<String, dynamic> dataMap) {
    return KiteExchangeEntity(
      uniqueIdentifier: dataMap['uniqueIdentifier'] ?? '',
      exchangeDescription: dataMap['exchangeDescription'] ?? '',
      mediaResourceUrl: dataMap['mediaResourceUrl'] ?? '',
      mediaType: MediaType.values.firstWhere(
        (type) => type.name == dataMap['mediaType'],
        orElse: () => MediaType.image,
      ),
      thumbnailResourceUrl: dataMap['thumbnailResourceUrl'],
      publisherPersonaId: dataMap['publisherPersonaId'] ?? '',
      publishTimestamp: DateTime.fromMillisecondsSinceEpoch(
        dataMap['publishTimestamp'] ?? DateTime.now().millisecondsSinceEpoch,
      ),
      totalCommentCount: dataMap['totalCommentCount'] ?? 0,
    );
  }

  /// 转换为Map数据
  Map<String, dynamic> convertToDataMap() {
    return {
      'uniqueIdentifier': uniqueIdentifier,
      'exchangeDescription': exchangeDescription,
      'mediaResourceUrl': mediaResourceUrl,
      'mediaType': mediaType.name,
      'thumbnailResourceUrl': thumbnailResourceUrl,
      'publisherPersonaId': publisherPersonaId,
      'publishTimestamp': publishTimestamp.millisecondsSinceEpoch,
      'totalCommentCount': totalCommentCount,
    };
  }

  /// 复制并修改部分属性
  KiteExchangeEntity duplicateWithModifications({
    String? uniqueIdentifier,
    String? exchangeDescription,
    String? mediaResourceUrl,
    MediaType? mediaType,
    String? thumbnailResourceUrl,
    String? publisherPersonaId,
    DateTime? publishTimestamp,
    int? totalCommentCount,
  }) {
    return KiteExchangeEntity(
      uniqueIdentifier: uniqueIdentifier ?? this.uniqueIdentifier,
      exchangeDescription: exchangeDescription ?? this.exchangeDescription,
      mediaResourceUrl: mediaResourceUrl ?? this.mediaResourceUrl,
      mediaType: mediaType ?? this.mediaType,
      thumbnailResourceUrl: thumbnailResourceUrl ?? this.thumbnailResourceUrl,
      publisherPersonaId: publisherPersonaId ?? this.publisherPersonaId,
      publishTimestamp: publishTimestamp ?? this.publishTimestamp,
      totalCommentCount: totalCommentCount ?? this.totalCommentCount,
    );
  }

  /// 检查是否为有效交换数据
  bool validateEntityIntegrity() {
    return uniqueIdentifier.isNotEmpty &&
        exchangeDescription.isNotEmpty &&
        mediaResourceUrl.isNotEmpty &&
        publisherPersonaId.isNotEmpty;
  }

  /// 检查是否为视频类型
  bool get isVideoType => mediaType == MediaType.video;

  /// 获取默认风筝交换数据
  static List<KiteExchangeEntity> getDefaultKiteExchanges() {
    final now = DateTime.now();

    return [
      KiteExchangeEntity(
        uniqueIdentifier: 'kite_exchange_001',
        exchangeDescription:
            "Trade my 'slightly tangled' kite for your 'totally perfect' one? 😂🪁",
        mediaResourceUrl: 'assets/images/kite_exchange/kite_exchange1.png',
        mediaType: MediaType.image,
        publisherPersonaId: 'kite_user_001', // SkyFly
        publishTimestamp: now.subtract(const Duration(hours: 48)),
        totalCommentCount: 4,
      ),
      KiteExchangeEntity(
        uniqueIdentifier: 'kite_exchange_002',
        exchangeDescription:
            "Got a spare kite? Let's swap! Mine's a rainbow delta—looking for something new. 🪁✨",
        mediaResourceUrl: 'assets/images/kite_exchange/kite_exchange2.png',
        mediaType: MediaType.image,
        publisherPersonaId: 'kite_user_002', // Windy
        publishTimestamp: now.subtract(const Duration(hours: 36)),
        totalCommentCount: 5,
      ),
      KiteExchangeEntity(
        uniqueIdentifier: 'kite_exchange_003',
        exchangeDescription:
            "Send me your kite, get mine—let's surprise each other! 🎁🪁",
        mediaResourceUrl: 'assets/images/kite_exchange/kite_exchange3.png',
        mediaType: MediaType.image,
        publisherPersonaId: 'kite_user_003', // Soarer
        publishTimestamp: now.subtract(const Duration(hours: 30)),
        totalCommentCount: 3,
      ),
      KiteExchangeEntity(
        uniqueIdentifier: 'kite_exchange_004',
        exchangeDescription:
            "My old kite has stories—wanna trade for one with new tales? 🪁💬",
        mediaResourceUrl: 'assets/images/kite_exchange/kite_exchange4.png',
        mediaType: MediaType.image,
        publisherPersonaId: 'kite_user_004', // Breeze
        publishTimestamp: now.subtract(const Duration(hours: 24)),
        totalCommentCount: 4,
      ),
      KiteExchangeEntity(
        uniqueIdentifier: 'kite_exchange_005',
        exchangeDescription:
            "Kite swap anyone? Mine flies like a dream but needs a new adventure! 🪁🌟",
        mediaResourceUrl: 'assets/images/kite_exchange/kite_exchange5.png',
        mediaType: MediaType.image,
        publisherPersonaId: 'kite_user_005', // Glider
        publishTimestamp: now.subtract(const Duration(hours: 18)),
        totalCommentCount: 5,
      ),
      KiteExchangeEntity(
        uniqueIdentifier: 'kite_exchange_006',
        exchangeDescription:
            "Bored of my kite's style—let's trade for something with more flair! 🪁💃",
        mediaResourceUrl: 'assets/images/kite_exchange/kite_exchange6.png',
        mediaType: MediaType.image,
        publisherPersonaId: 'kite_user_001', // SkyFly
        publishTimestamp: now.subtract(const Duration(hours: 12)),
        totalCommentCount: 3,
      ),
    ];
  }
}

/// 媒体类型枚举
enum MediaType { image, video }
